package com.google.code.bankagregator.adapter.bank.alpha.parser.deposit;

import com.google.code.bankagregator.adapter.bank.alpha.parser.FuncForDepParsers;
import com.google.code.bankagregator.adapter.exception.AdapterConnectException;
import com.google.code.bankagregator.entities.Currency;
import com.google.code.bankagregator.entities.Deposit;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: kirill
 * Date: 08.06.13
 * Time: 0:35
 * To change this template use File | Settings | File Templates.
 */
public class AlphaDepMegaProfitParser {
    private final String DEPOSIT_URL = "http://www.alfabank.com.ua/retail/deposits/tariffs/program/megaprofit/";
    private Document con = null;
    private String minAmountStr;
    private Integer[] tableLinesWithTerm;
    private static final int ROW_BEGIN_FOR_UAH = 1;
    private static final int ROW_BEGIN_FOR_USD = 2;
    private static final int ROW_BEGIN_FOR_EUR = 3;

    public AlphaDepMegaProfitParser() throws AdapterConnectException {
        tableLinesWithTerm = new Integer[]{5, 6, 7, 8, 9, 10, 11};
        minAmountStr = new FuncForDepParsers(DEPOSIT_URL).getMinAmount();
        try {
            con = Jsoup.connect(DEPOSIT_URL).timeout(20000).get();
        } catch (IOException e) {
            throw new AdapterConnectException(e.getMessage());
        }
    }

    /**
     * Get deposit`s params for UAH
     *
     * @return deposit`s params
     * @throws AdapterConnectException
     */
    public List<Deposit> getUAHDeposit() throws AdapterConnectException {
        int minAmount = Integer.parseInt(minAmountStr.substring(26, 31).replace(" ", ""));
        List<Deposit> depositParam = new FuncForDepParsers(DEPOSIT_URL).parser(minAmount,
                ROW_BEGIN_FOR_UAH, Currency.UAH, tableLinesWithTerm, DEPOSIT_URL);
        return depositParam;

    }

    /**
     * Get deposit`s params for USD
     *
     * @return deposit`s params
     * @throws AdapterConnectException
     */
    public List<Deposit> getUSDDeposit() throws AdapterConnectException {
        int minAmount = Integer.parseInt(minAmountStr.substring(36, 40).replace(" ", ""));
        List<Deposit> depositParam = new FuncForDepParsers(DEPOSIT_URL).parser(minAmount,
                ROW_BEGIN_FOR_USD, Currency.USD, tableLinesWithTerm, DEPOSIT_URL);
        return depositParam;

    }

    /**
     * Get deposit`s param for EUR
     *
     * @return deposit`s params
     * @throws AdapterConnectException
     */
    public List<Deposit> getEURDeposit() throws AdapterConnectException {
        int minAmount = Integer.parseInt(minAmountStr.substring(52, 55).replace(" ", ""));
        List<Deposit> depositParam = new FuncForDepParsers(DEPOSIT_URL).parser(minAmount,
                ROW_BEGIN_FOR_EUR, Currency.EUR, tableLinesWithTerm, DEPOSIT_URL);
        return depositParam;
    }
}
